import { createSlice } from "@reduxjs/toolkit"
import {
    setToken as _setToken,
    getStudentInformation,
    setStudentInformation,
    removeStudentInformation,
    setCoachInformation,
    getCoachInformation,
    removeCoachInformation,
    getSearchStudentInformation,
    setSearchStudentInformation,
    removeSearchStudentInformation,
    setSearchCoachInformation,
    getSearchCoachInformation,
    removeSearchCoachInformation
 } from "@/utils"
import { getAppliAPI, searchUserInfoAPI, setUserInfoAPI } from "@/api/admin"
const adminInfoStore = createSlice({
    name:'adminInfo',
    //数据状态
    initialState:{
        appliInfo:{},
        studentInitInfo:getStudentInformation()||'',
        coachInitInfo:getCoachInformation()||'',
        searchStudentInfo:getSearchStudentInformation()||'',
        searchCoachInfo:getSearchCoachInformation()||''
    },
    //同步修改方法
    reducers:{
        //登录设置token
        setAppliInfo(state,action){
            state.appliInfo = action.payload
            console.log("数据：",action.payload)
        },
        //设置学员、教练员初始录入信息记录
        setUserInitInfo(state,action)
        {
            if(action.payload.data.type === 'student')
            {
                state.studentInitInfo=action.payload.data
                setStudentInformation(action.payload.data)
                console.log(state.studentInitInfo)
            }else{
                state.coachInitInfo = action.payload.data
                setCoachInformation(action.payload.data)
            }
            
        },
        //设置搜素信息
        setSearchUserInfo(state,action)
        {
            if(action.payload.data.type === 'student')
            {
                state.searchStudentInfo=action.payload.data
                setSearchStudentInformation(action.payload.data)
            }else{
                state.searchCoachInfo = action.payload.data
                setSearchCoachInformation(action.payload.data)
            }
        },
        //清除登录、缓存信息
        clearAdminInfo(state){
            state.appliInfo =[]
            removeStudentInformation()
            removeCoachInformation()
            removeSearchStudentInformation()
            removeSearchCoachInformation()
            console.log("清除了")
            window.location.reload()
        }
    }
})

//解构actionCreater
const {clearAdminInfo,setAppliInfo,setUserInitInfo,setSearchUserInfo} = adminInfoStore.actions

//获取reducer函数

const adminInfoReducer = adminInfoStore.reducer

//异步方法封装
//获取审批列表
const fetchGetAppliInfo =() =>{
    return async(dispatch)=>{
        //1.发送异步请求
        const res = await getAppliAPI()
        console.log("res:",res)
        dispatch(setAppliInfo(res))
    }
}
//初始化用户基本信息并获取
const fetchUserInitInfo =(data,type) =>{
    return async(dispatch)=>{
        //1.发送异步请求
        const res = await setUserInfoAPI(data,type)
        console.log("res122:",res)
        dispatch(setUserInitInfo(res))
    }
}

//搜索教练、学员信息
const fetchSearchUserInfo =(data,type) =>{
    return async(dispatch)=>{
        //1.发送异步请求
        const res = await searchUserInfoAPI(data,type)
        console.log("res122:",res)
        dispatch(setSearchUserInfo(res))
    }
}

export {clearAdminInfo,fetchGetAppliInfo,fetchUserInitInfo,fetchSearchUserInfo}

export default adminInfoReducer